package com.guigu.basedservice.utils;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**
 * @author heyuhang
 * @create 2024/9/26
 */
@Component
public class UniqueIdGenerator {
    private static final String PREFIX = "4028858279540fe301798";
    private static final int RANDOM_PART_LENGTH = 11;
    private static final Set<String> generatedIds = new HashSet<>();
    private static final Random random = new Random();

    public static String generateUniqueId() {
        String uniqueId;
        do {
            StringBuilder sb = new StringBuilder(PREFIX);
            for (int i = 0; i < RANDOM_PART_LENGTH; i++) {
                sb.append(random.nextInt(10)); // 生成0-9的随机数
            }
            uniqueId = sb.toString();
        } while (generatedIds.contains(uniqueId));

        generatedIds.add(uniqueId);
        return uniqueId;
    }

    public static void main(String[] args) {
        // 测试生成唯一编号
        for (int i = 0; i < 10; i++) {
            System.out.println(generateUniqueId());
        }
    }
}
